<?php
namespace app\admin\controller;




use app\admin\model\Adminmodel;

class Admin extends Rbac
{
//    管理员管理页面
    public function admin()
    {
//        实例化adminmodel
        $admin=new Adminmodel();
//       调用模型中的select 方法
        $result=$admin->select();
        $this->assign(["result"=>$result]);

       return $this->fetch('admin');
    }

//    添加管理员页面
    public function insert(){
        $request=request()->param();
        if(!empty($request)){
            $name=$request['user_name'];
//        判断管理员是否存在
            $exit=db("user")->where("user_name='$name'")->select();
            if($exit){
                $this->success("管理员已存在！","admin/admin/insert");
            }else{
                $list = [
                    'user_name'=>$name,
                    'user_r_id'=>$request['user_r_id'],
                    'user_password'=>sha1(md5("z123456")."mingdaima"),
                    'user_createtime'=>time()
                ];
                $result=db('user')->insert($list);
                if($result){
                    $this->success("添加成功,默认登录密码为：z123456","admin/admin/admin");
                }
            }
        }
        return $this->fetch("admininsert");
    }
//    伪删除
    public function delete(){
        $id=request()->param("id");
        $st=request()->param("dell");
        $result=db("user")->where("user_id='$id'")->update([
            "user_dell"=>$st
        ]);
        return $result;
    }
//    修改
    public function update(){
        if(!empty($_POST)){
            var_dump($_POST);
            $data=[
                'user_name'=>$_POST['user_name'],
                'user_r_id'=>$_POST['user_r_id']
            ];
            $id=$_POST['user_id'];
            $result=db("user")->where("user_id='$id'")->update($data);
            if($result){
                $this->success("修改成功","admin/admin/admin");
            }else{
                $this->error("您未做任何更改！","admin/admin/admin");
            }
        }

//        根据id实现回显
        $id=request()->param("id");
        $result=db("user")->where("user_id='$id'")->find();
        $this->assign(["result"=>$result]);
        return $this->fetch("adminupdate");
    }
//    重置密码
    public function reset(){
        $id=request()->param("id");
        $pass=db("user")->field('user_password')->where("user_id='$id'")->find();
        if($pass["user_password"]=="z123456"){
            $this->success("您的密码已重置！");
        }else{
            $result=db("user")->where("user_id='$id'")->update(['user_password'=>"z123456"]);
            if($result){
                $this->success("重置成功");
            }
            else{
                $this->error("重置失败");
            }
        }

    }

//    根据名字搜索
    public function namep(){
        $name=request()->param('name');
        if(!empty($name)){
            $result=db("user")->where("user_name like '%$name%'")->paginate(10,false,['path'=>"/crm/public/index.php/admin/role/role"]);
            if(empty($result[0]["user_id"])){
                echo "<script>alert('用户名不存在！显示初始列表')</script>";
                $result=db("user")->paginate(10,false,['path'=>"/crm/public/index.php/admin/role/role"]);
            }

        }else{
            $result=db("user")->paginate(10,false,['path'=>"/crm/public/index.php/admin/role/role"]);
        }

        $this->assign(["result"=>$result]);
        return $this->fetch('admin');
    }
}
